var jq = jQuery.noConflict();

$(function () {

    var name = getQueryString("name");
    //判断搜索名
    if (name != null && name != "") {
        solr(1, name, 2, 0);
    }/*else {
        solr(1,"手机",4,1);
    }*/
    //头部分类
    $("#tou").load("Header.html", function () {
        $(".leftNav").attr("class", "leftNav none");

        $(".nav").hover(function () {
            $(".leftNav").attr("class", "leftNav");
        }, function () {
            $(".leftNav").attr("class", "leftNav none");
        })

    });
    //商品内容加载
    var id = getQueryString("id");
    if (id != null && id != "") {
        neirong(id, 1, 5);
    }

    collect();

    //清空我的收藏
    $("#clear").click(function () {
        $.ajax({
            url: "easybuy/clearCollect",
            success: function (data) {
                if (data == "success") {
                    $("#favoritesPro").empty();
                    $("#favoritesPro").html("<div style=\"width: 209px;height: 200px;margin-top: 80px\">\n" +
                        "                    <font style=\"color: #ff4e00;font-size: 14px\">收藏夹暂无商品，赶紧去收藏宝贝吧！</font>\n" +
                        "                </div>");
                }
            }
        })
    })
});

//solr查询商品分页
function solr(pageNum, name, pageSize, status) {
    $.ajax({
        url: "easybuy/selectProductBySolr",
        data: {
            name: name,
            pageNum: pageNum,
            pageSize: pageSize,
            status: status
        },
        success: function (data) {

            $("#name").val(data.name);
            $("#productCount").text(data.numFound);

            $(".cate_list").empty();
            if (data.list!=0) {//商品列表不为空
                $.each(data.list, function (index, cate) {

                    $(".cate_list").append("<li>\n" +
                        "                    \t<div class=\"img\"><a href=\"Product.html?id=" + cate.id + "\"><img src=\"http://47.104.137.246:9999/files/" + cate.filename + "\"  width=\"210\" height=\"185\" /></a></div>\n" +
                        "                        <div class=\"price\">\n" +
                        "                            <font>￥<span>" + cate.price + "</span></font> &nbsp; 26R\n" +
                        "                        </div>\n" +
                        "                        <div class=\"name\"><a href=\"Product.html?id=" + cate.id + "\">" + cate.name + "</a></div>\n" +
                        "                        <div class=\"carbg\">\n" +
                        "                        \t<a href=\"#\" class=\"ss\"data-id=" + cate.id + ">收藏</a>\n" +
                        "                            <a href=\"javascript:;\" class=\"j_car\" data-id='" + cate.id + "'>加入购物车</a>\n" +
                        "                        </div>\n" +
                        "                    </li>");
                });
            }else {
                $(".cate_list").append("<h1>暂无相关宝贝，先去看看其它商品吧！</h1>");
                $(".pages").empty();
            }


            //商品加购
            addtocar();
            //商品收藏
            addToFavorites()
            if (data.list!=0) {

                $(".pages").empty();

                //首页
                $(".pages").append("<a href=\"javascript:;\" class=\"p_pre\" id='homePage'>首页</a>");

                $("#homePage").click(function () {
                    solr(1,data.name,pageSize,1);
                });


                //页数
                for (let i = 0; i < data.navigatepageNums.length; i++) {
                    if (pageNum == i + 1) {
                        $(".pages").append("<a href=\"javascript:;\" class=\"cur number\">" + data.navigatepageNums[i] + "</a>");
                    } else {
                        $(".pages").append("<a href=\"javascript:;\" class='number'>" + data.navigatepageNums[i] + "</a>");
                    }
                }
                $(".number").click(function () {
                    var pageNum = $(this).text();
                    solr(pageNum, data.name, pageSize, 1);
                })

                //末页
                $(".pages").append("<a href=\"javascript:;\" class=\"p_pre\" id='lastPage'>末页</a>");

                $("#lastPage").click(function () {
                    solr(data.count,data.name,pageSize,1)
                });
            }

        }
    });

}

//商品的列表
function neirong(id, pageNum, pageSize) {

    if (id == "" || id == null) {
        id = -1;
    }

    $.ajax({
        url: "easybuy/catelist",
        data: {
            categoryLevelId: id,
            pageNum: pageNum,
            pageSize: pageSize
        },
        success: function (data) {
            $("#productCount").text(data.total);
            $(".cate_list").empty();

            if (data.list!=0){//商品列表不为空
                $.each(data.list, function (index, cate) {
                $(".cate_list").append("<li>\n" +
                    "                    \t<div class=\"img\"><a href=\"Product.html?id=" + cate.id + "\"><img src=\"http://47.104.137.246:9999/files/" + cate.filename + "\" width=\"210\" height=\"185\" /></a></div>\n" +
                    "                        <div class=\"price\">\n" +
                    "                            <font>￥<span>" + cate.price + "</span></font> &nbsp; 26R\n" +
                    "                        </div>\n" +
                    "                        <div class=\"name\"><a href=\"Product.html?id=" + cate.id + "\">" + cate.name + "</a></div>\n" +
                    "                        <div class=\"carbg\">\n" +
                    "                        \t<a href=\"#\" class=\"ss\"data-id=" + cate.id + ">收藏</a>\n" +
                    "                            <a href=\"javascript:;\" class=\"j_car\" data-id='" + cate.id + "'>加入购物车</a>\n" +
                    "                        </div>\n" +
                    "                    </li>");
            });
            }else {
                $(".cate_list").append("<h1>暂无相关宝贝，先去看看其它商品吧！</h1>");
            }
            //商品加购
            addtocar();
            //商品收藏
            addToFavorites()
            /////////////////////////////////
            if (data.list!=0){//商品列表不为空
                getPage(id,data,pageNum,pageSize);
            }
            //////////////////////////////////////////
        }
    });


}

//加购
function addtocar() {
    $(".j_car").click(function () {
        var id = $(this).data("id");
        $.ajax({
            url: "easybuy/caradd",
            data: {
                productId: id
            },
            success: function (a) {
                if (a == "userEmpty") {
                    alert("请先登录！");
                } else {
                    alert("添加成功！");
                    car();
                }
            }
        })
    });
}

//加收藏
function addToFavorites() {
    $(".ss").click(function () {
        var id = $(this).data("id");
        $.ajax({
            url: "easybuy/favoritesadd",
            data: {
                productId: id
            },
            success: function (result) {
                if (result == "userEmpty") {
                    alert("请先登录！");
                } else if (result == "exists") {
                    alert("您已收藏过该商品");
                } else {
                    alert("添加成功！");
                    car();
                    collect();
                }
            }
        })
    });

}

//重新加载购物车
function car(){
    $.ajax({
        url:"easybuy/carprolist",
        success:function (param){
            var products = param.products;
            var prices = param.prices;
            if (products!=""&&products!=null){
                var countsum = 0;
                $(".cars").empty();
                $(".prices").empty();
                $.each(products,function (index,product) {
                    $(".cars").append("" +
                        "                <li>\n" +
                        "                    <div class=\"img\"><a href=\"#\"><img src=\"http://47.104.137.246:9999/files/"+product.filename+"\" width=\"58\" height=\"58\" /></a></div>\n" +
                        "                    <div class=\"name\"><a href=\"#\">"+product.name+"</a></div>\n" +
                        "                    <div class=\"price\"><font color=\"#ff4e00\">￥"+product.price+"</font> X"+product.carcount+"</div>\n" +
                        "                </li>");
                    countsum = countsum + product.carcount;
                });
                $(".prices").append("" +
                    "<div class=\"price_sum\">共计&nbsp; <font color=\"#ff4e00\">￥</font><span>"+prices+"</span></div>\n" +
                    "            <div class=\"price_a\"><a href=\"BuyCar.html\">去购物车结算</a></div>");
                $(".car_t").children("span").html(products.length);
            }

        }
    })
}

//我的收藏列表加载
function collect() {
    $("#favoritesPro").empty();
    $.ajax({
        url:"easybuy/collectlist",
        success:function (list) {
            if (list == null || list.length==0) {
                $("#favoritesPro").html("<div style=\"width: 209px;height: 200px;margin-top: 80px\">\n" +
                    "                    <font style=\"color: #ff4e00;font-size: 14px\">收藏夹暂无商品，赶紧去收藏宝贝吧！</font>\n" +
                    "                </div>");
            }else {
                var j = 0;
                $.each(list,function (index) {
                    var product=list[list.length-index-1];

                    $("#favoritesPro").append("<li>\n" +
                        "                    <div class=\"img\"><a href=\"Product.html?id=" + product.id + "\"><img src=\"http://47.104.137.246:9999/files/" + product.filename + "\" width=\"185\" height=\"162\" /></a></div>\n" +
                        "                \t<div class=\"name\"><a href=\"Product.html?id=" + product.id + "\">" + product.name + "</a></div>\n" +
                        "                    <div class=\"price\">\n" +
                        "                    \t<font>￥<span class='a'>" + product.price + "</span>&nbsp;&nbsp;&nbsp;&nbsp;" +
                        "                    <span class='arrows'></span>&nbsp;<span class='difference' style='color: red'></span></font>\n" +
                        "                    </div>\n" +
                        "                </li>");

                    var a;
                    $.ajax({
                        url:"easybuy/product",
                        data:{
                            productId:product.id
                        },
                        success:function (data) {
                            if (product.price!=data.price){
                                a=data.price-product.price;
                                if (a>0){
                                    $(".arrows").eq(index).attr("class","arrows glyphicon glyphicon-arrow-up");
                                    $(".difference").eq(index).text(+a);
                                }else if (a<0){
                                    $(".arrows").eq(index).attr("class","arrows glyphicon glyphicon-arrow-down");
                                    $(".difference").eq(index).text(Math.abs(a));
                                }
                                $(".price").find(".a").eq(index).text(data.price);
                            }
                        }
                    })
                    if (index == 4) {
                       return  false;
                    }
                });
            }
        }
    })



}

//分页
function getPage(id,data,pageNum,pageSize) {
    $(".pages").empty();
    //首页
    $(".pages").append("<a href=\"javascript:;\" class=\"p_pre\" id='homePage'>首页</a>");

    $("#homePage").click(function () {
        neirong(id,1,pageSize);
    });

    //页数
    for (let i = 0; i < data.navigatepageNums.length; i++) {
        if (pageNum == i+1) {
            $(".pages").append("<a href=\"javascript:;\" class=\"cur number\">" + (data.navigatepageNums[i]) + "</a>");
        } else {
            $(".pages").append("<a href=\"javascript:;\" class='number'>" + (data.navigatepageNums[i]) + "</a>");
        }
    }
    $(".number").click(function () {
        var pageNum=$(this).text();
        neirong(id,pageNum,pageSize)
    })

    //末页
    $(".pages").append("<a href=\"javascript:;\" class=\"p_pre\" id='lastPage'>末页</a>");

    $("#lastPage").click(function () {
        neirong(id,data.pages,pageSize);
    });
}

//取得地址栏参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg); //r是一个数组
    if (r != null) return unescape(r[2]);
    return null;
}